﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Mahasiswa.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            RunAsync().Wait();
            System.Console.ReadKey();
        }

        static async Task RunAsync()
        {
            string serviceUrl = "api/Mahasiswa/";
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8492/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // Input data mahasiswa baru
                Mahasiswa mahasiswa = new Mahasiswa();
                mahasiswa.NIM = "23";
                mahasiswa.Nama = "Nama 23";
                mahasiswa.Umur = 23;
                mahasiswa.Alamat = "Alamat 23";
                mahasiswa.Jurusan = "Ilkom";

                HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, mahasiswa).Result;
                if (response.IsSuccessStatusCode)
                {
                    System.Console.WriteLine("Data mahasiswa berhasil disimpan.");
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }

                System.Console.WriteLine();

                // Menampilkan data mahasiswa
                response = await client.GetAsync(serviceUrl);
                if (response.IsSuccessStatusCode)
                {
                    var mhs = response.Content.ReadAsAsync<IEnumerable<Mahasiswa>>().Result;
                    foreach (Mahasiswa m in mhs)
                    {
                        System.Console.WriteLine(m.Nama);
                    }
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }

                System.Console.WriteLine();

                // Update data mahasiswa
                mahasiswa = new Mahasiswa();
                mahasiswa.NIM = "24";
                mahasiswa.Nama = "Nama 24";
                mahasiswa.Umur = 24;
                mahasiswa.Alamat = "Alamat 24";
                mahasiswa.Jurusan = "Ilkom";

                response = client.PutAsJsonAsync(serviceUrl + "?nim=23", mahasiswa).Result;
                if (response.IsSuccessStatusCode)
                {
                    System.Console.WriteLine("Data mahasiswa berhasil diupdate.");
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }

                System.Console.WriteLine();

                // Menampilkan data mahasiswa
                response = await client.GetAsync(serviceUrl);
                if (response.IsSuccessStatusCode)
                {
                    var mhs = response.Content.ReadAsAsync<IEnumerable<Mahasiswa>>().Result;
                    foreach (Mahasiswa m in mhs)
                    {
                        System.Console.WriteLine(m.Nama);
                    }
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }

                System.Console.WriteLine();

                // Hapus data mahasiswa
                response = client.DeleteAsync(serviceUrl + "?nim=23").Result;
                if (response.IsSuccessStatusCode)
                {
                    System.Console.WriteLine("Data mahasiswa berhasil dihapus.");
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }

                System.Console.WriteLine();

                // Menampilkan data mahasiswa
                response = await client.GetAsync(serviceUrl);
                if (response.IsSuccessStatusCode)
                {
                    var mhs = response.Content.ReadAsAsync<IEnumerable<Mahasiswa>>().Result;
                    foreach (Mahasiswa m in mhs)
                    {
                        System.Console.WriteLine(m.Nama);
                    }
                }
                else
                {
                    System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }
            }
        }
    }

    public class Mahasiswa
    {
        public string NIM { set; get; }
        public string Nama { set; get; }
        public int Umur { set; get; }
        public string Alamat { set; get; }
        public string Jurusan { set; get; }
    }
}
